package com.basic.business.finance.domain;

import cn.idev.excel.annotation.ExcelIgnoreUnannotated;
import cn.idev.excel.annotation.ExcelProperty;
import com.basic.common.utils.StringUtils;
import com.basic.common.utils.uuid.ULID;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 聚合账单明细
 */
@Setter
@Getter
@ExcelIgnoreUnannotated
public class TAgcPayMoneyDetails implements Serializable {

    private static final long serialVersionUID = -6509253631491255065L;

    private String id;

    /** 主表id */
    private String mainId;

    /** 收入金额 */
    @ExcelProperty(value = "收入金额（元）")
    private String incomeAmountStr;

    private BigDecimal incomeAmount = BigDecimal.ZERO;

    /** 支出金额 */
    @ExcelProperty(value = "支出金额")
    private String expenditureAmountStr;

    private BigDecimal expenditureAmount = BigDecimal.ZERO;

    /** 业务类型 */
    @ExcelProperty(value = "入账类型")
    private String businessType;

    /** 业务类型备注 */
    @ExcelProperty(value = "备注")
    private String businessTypeRemark;

    @ExcelProperty(value = "入账时间")
    private Date createTime;

    public void setId(String id) {
        this.id = StringUtils.isNotEmpty(id) ? id : ULID.getId();
    }

    public TAgcPayMoneyDetails() {
        this.id = ULID.getId();
    }

    public void setIncomeAmountStr(String amountStr) {
        if (StringUtils.isBlank(amountStr)) {
            this.incomeAmount = BigDecimal.ZERO;
            return;
        }
        try {
            // 去掉所有非数字、非小数点、非负号的字符
            String cleanStr = amountStr
                    .replaceAll("[^\\d.-]", ""); // 只保留数字、小数点、负号
            BigDecimal parsedAmount = new BigDecimal(cleanStr);
            this.incomeAmount = parsedAmount.abs(); // 强制转为正数
        } catch (NumberFormatException e) {
            this.incomeAmount = BigDecimal.ZERO;
        }
    }

    public void setExpenditureAmountStr(String amountStr) {
        if (StringUtils.isBlank(amountStr)) {
            this.expenditureAmount = BigDecimal.ZERO;
            return;
        }
        try {
            // 去掉所有非数字、非小数点、非负号的字符
            String cleanStr = amountStr
                    .replaceAll("[^\\d.-]", ""); // 只保留数字、小数点、负号
            BigDecimal parsedAmount = new BigDecimal(cleanStr);
            this.expenditureAmount = parsedAmount.signum() < 0
                    ? parsedAmount               // 已经是负数就不再取反
                    : parsedAmount.negate();     // 正数则变负
        } catch (NumberFormatException e) {
            this.expenditureAmount = BigDecimal.ZERO;
        }
    }

    @Override
    public String toString() {
        return "TAgcPayMoneyDetails{" +
                "id='" + id + '\'' +
                ", mainId='" + mainId + '\'' +
                ", incomeAmount=" + incomeAmount +
                ", expenditureAmount=" + expenditureAmount +
                ", businessType='" + businessType + '\'' +
                ", businessTypeRemark='" + businessTypeRemark + '\'' +
                ", createTime=" + createTime +
                '}';
    }
}
